package com.hhsjtest.alipaydragview.impl;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hhsjtest.appdraglayout.alipay.AlipayDragFrameLayout;
import com.hhsjtest.appdraglayout.alipay.DragItemView;
import com.hhsjtest.alipaydragview.R;

import java.util.ArrayList;
import java.util.List;

public class TestAlipayLayoutActivity extends AppCompatActivity {

    private CustomFrameLayout layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_alipay_activity);
        layout = findViewById(R.id.custom_layout);
        findViewById(R.id.add_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.addData(new ItemInfo(false, "item " + layout.getChildCount(), layout.getChildCount()));
            }
        });


        findViewById(R.id.add_15).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ItemInfo> itemInfos = new ArrayList<>();
                for (int i = 0; i < 15; i++) {
                    itemInfos.add(new ItemInfo(false, "item " + i, i));
                }
                layout.setDataList(itemInfos);
            }
        });

        findViewById(R.id.remove_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.removeLastData();
            }
        });

        layout.setRowColumnNumber(3, 4);


        findViewById(R.id.mode_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.setEditMode(true);
            }
        });

        findViewById(R.id.mode_normal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.setEditMode(false);
            }
        });
        layout.setDragShadowLayoutParent(findViewById(R.id.drag_layout_parent));
        layout.setDragItemFactory(new DefaultDragItemFactory(this));
        layout.setDragFloatListener(new AlipayDragFrameLayout.DragFloatListener<ItemInfo>() {
            @Override
            public void showFloatView(ItemInfo data, int position, int left, int top) {
                DragItemView<ItemInfo> itemView = findViewById(R.id.float_view_2);
                itemView.setEdit(true);
                itemView.setVisibility(View.VISIBLE);
                itemView.updateData(data);

                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) itemView.getLayoutParams();
                layoutParams.width = layout.getItemWidth();
                layoutParams.height = layout.getItemHeight();
                layoutParams.leftMargin = left;
                layoutParams.topMargin = layout.getTop() + top - layout.getScrollParent().getScrollY();
                itemView.getParent().requestLayout();

            }

            @Override
            public void updateFloatView(int left, int top) {
                DefaultDragItemView itemView = findViewById(R.id.float_view_2);
                itemView.setEdit(true);
                ((ViewGroup.MarginLayoutParams) itemView.getLayoutParams()).leftMargin = left;
                ((ViewGroup.MarginLayoutParams) itemView.getLayoutParams()).topMargin = top;
                itemView.getParent().requestLayout();

            }

            @Override
            public void hideFloatView() {
                DefaultDragItemView itemView = findViewById(R.id.float_view_2);
                itemView.setVisibility(View.GONE);
            }
        });

    }
}
